from django.shortcuts import render
from django.views.generic import View
from django.http import JsonResponse
from django.db.models import Q

from .models import Course, CourseResource, Video
from operation.models import CourseComments, UserCourse
from pure_pagination import Paginator, EmptyPage, PageNotAnInteger
from utils.mixin_utils import LoginRequiredMixin


# Create your views here.


class CourseListView(View):
    def get(self, request):
        course_list = Course.objects.all().order_by('-add_time')

        hot_courses = Course.objects.all().order_by('-click_nums')[:3]

        # 搜索
        search_keywords = request.GET.get('keywords', '')
        if search_keywords:
            course_list = course_list.filter(Q(name__icontains=search_keywords) | Q(
                detail__icontains=search_keywords) | Q(desc__icontains=search_keywords))

        # 排行
        try:
            sort = request.GET.get('sort', '')
        except Exception:
            sort = None
        if sort == 'hot':
            course_list = course_list.order_by('-click_nums')
        elif sort == 'students':
            course_list = course_list.order_by('-students')

        # 对课程机构进行分类
        try:
            page = request.GET.get('page', 1)
        except PageNotAnInteger:
            page = 1

        # Provide Paginator with the request object for complete querystring generation
        p = Paginator(course_list, request=request, per_page=3)

        courses = p.page(page)

        return render(request, 'course-list.html', {
            "course_list": courses,
            "sort": sort,
            "hot_courses": hot_courses
        })


class CourseDetailView(View):
    def get(self, request, course_id):
        course = Course.objects.filter(id=int(course_id)).first()
        course.click_nums += 1
        course.save()

        tag = course.tag
        if tag:
            relate_courses = Course.objects.filter(tag=tag)[:2]
        else:
            relate_courses = None
        return render(request, 'course-detail.html', {
            "course": course,
            "relate_courses": relate_courses
        })


class CourseInfoView(LoginRequiredMixin, View):
    def get(self, request, course_id):
        course = Course.objects.filter(id=int(course_id)).first()
        course.students += 1
        course.save()
        user_course = UserCourse.objects.filter(user=request.user, course=course)
        if not user_course:
            UserCourse.objects.create(user=request.user, course=course)
        all_resourses = CourseResource.objects.filter(course=course)
        return render(request, 'course-video.html', {
            "course": course,
            "all_resourses": all_resourses
        })


class CommentsView(LoginRequiredMixin, View):
    def get(self, request, course_id):
        course = Course.objects.filter(id=int(course_id)).first()
        all_resourses = CourseResource.objects.filter(course=course)
        all_comments = CourseComments.objects.filter(course=course)
        return render(request, 'course-comment.html', {
            "course": course,
            "all_resourses": all_resourses,
            "all_comments": all_comments
        })


class AddCommentView(View):
    def post(self, request):
        course_id = request.POST.get('course_id', 0)
        comments = request.POST.get('comments', 0)
        if not request.user:
            return JsonResponse({'status': "fail", 'msg': "客户未登录"})

        if int(course_id) > 0 and comments:
            course = Course.objects.get(id=int(course_id))
            CourseComments.objects.create(course=course, comments=comments, user=request.user)
            return JsonResponse({'status': "success", 'msg': "添加成功"})
        else:
            return JsonResponse({'status': "fail", 'msg': "添加失败"})


class VideoPlayView(View):
    def get(self, request, video_id):
        video = Video.objects.filter(id=int(video_id)).first()
        course = video.lesson.course
        all_resourses = CourseResource.objects.filter(course=course)

        return render(request, 'course-play.html', {
            "course": course,
            'video': video,
            "all_resourses": all_resourses,
        })
